package com.lgqz.himalayafm.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.lgqz.himalayafm.DrawListener;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.fragment.BaseFragment;
import com.lgqz.himalayafm.fragment.DiscoverTabFragment;
import com.lgqz.himalayafm.fragment.LoadFragment;
import com.lgqz.himalayafm.fragment.MenuFragment;
import com.lgqz.himalayafm.fragment.RadioFragmentProvince;
import com.lgqz.himalayafm.fragment.RadioFragmentSlidingListView;
import com.lgqz.himalayafm.fragment.SubscribeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, DrawListener,
        DrawerLayout.DrawerListener {
    private RadioGroup radioGroup;
    private List<Fragment> fragmentList;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private DrawerLayout drawerLayout;
    private FrameLayout frameLayout;
    private FragmentTransaction fragmentTransaction;
    private RadioFragmentSlidingListView fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.tab_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.draw_main);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        drawerLayout.addDrawerListener(this);
        radioGroup.setOnCheckedChangeListener(this);
        fragmentList = new ArrayList<>();

        fragmentList.add(new DiscoverTabFragment());
        fragmentList.add(new SubscribeFragment());
        fragmentList.add(new LoadFragment());
        fragmentList.add(new MenuFragment());
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        transaction.add(R.id.container_main, fragmentList.get(0)).add(R.id.container_main, fragmentList.get(1)).add(R
                .id.container_main, fragmentList.get(2)).add(R.id.container_main, fragmentList.get(3)).show
                (fragmentList.get(0)).hide(fragmentList.get(1)).hide(fragmentList.get(2)).hide(fragmentList.get(3))
                .commit();
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        transaction = manager.beginTransaction();
        switch (i) {
            case R.id.discover_main:
                showFragment(0);
                break;
            case R.id.subscribe_main:
                showFragment(1);
                break;
            case R.id.down_main:
                showFragment(2);
                break;
            case R.id.myself_main:
                showFragment(3);
                break;


        }

        transaction.commit();
    }


    private void showFragment(int position) {
        for (int i = 0; i < fragmentList.size(); i++) {
            if (position == i) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }
        }
    }

    @Override
    public void openDraw(String tag, Bundle bundle) {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        FragmentManager manager = getSupportFragmentManager();
        fragmentTransaction = manager.beginTransaction();
        switch (tag) {
            case "11":
                fragmentTransaction.replace(R.id.main_draw_menu,
                        RadioFragmentSlidingListView.newInstance(bundle.getString("station")));
                break;
            case "12":
                fragmentTransaction.replace(R.id.main_draw_menu,
                        new RadioFragmentProvince());
                break;
            case "热门推荐":
                fragmentTransaction.replace(R.id.main_draw_menu, BaseFragment.newInstance(bundle.getLong("albumId")));
                break;
        }

        fragmentTransaction.commit();
        drawerLayout.openDrawer(Gravity.RIGHT);
    }

    @Override
    public void closeDraw() {
        drawerLayout.closeDrawer(Gravity.RIGHT);
    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(View drawerView) {

    }

    @Override
    public void onDrawerClosed(View drawerView) {

        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }
}
